package cn.tedu.yxcar.product.service.impl;

import cn.tedu.yxcar.product.ex.CoolSharkServiceException;
import cn.tedu.yxcar.product.mapper.ArticleMapper;
import cn.tedu.yxcar.product.pojo.dto.ArticleAddDTO;
import cn.tedu.yxcar.product.pojo.dto.ArticleUpdateDTO;
import cn.tedu.yxcar.product.pojo.entity.Article;
import cn.tedu.yxcar.product.pojo.vo.ArticleDetailsVO;
import cn.tedu.yxcar.product.pojo.vo.ArticleListVO;
import cn.tedu.yxcar.product.pojo.vo.ArticleSearchVO;
import cn.tedu.yxcar.product.pojo.vo.AuthorInfoVO;
import cn.tedu.yxcar.product.service.IArticleService;
import cn.tedu.yxcar.product.web.JsonPage;
import cn.tedu.yxcar.product.web.ResponseCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 处理文章的业务实现类
 * </p>
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void insert(ArticleAddDTO articleAddDTO) {
        // 调用参数对象的getTitle()得到尝试添加的文章的名称
        String title = articleAddDTO.getTitle();
        // 调用Mapper对象的countByName()执行统计查询
        int count = articleMapper.countByTitle(title);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加文章失败，文章名称已经被占用！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, message);
        }
        // 创建Article对象
        Article article = new Article();
        // 调用BeanUtils.copyProperties将参数对象中的属性复制到Article对象中
        BeanUtils.copyProperties(articleAddDTO, article);
        // 调用Mapper对象的insert()执行插入文章数据
        int rows = articleMapper.insert(article);
        if (rows != 1) {
            String message = "添加文章失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        // 调用Mapper的getStandardById()执行查询，检查数据是否不存在
        ArticleDetailsVO article = articleMapper.getStandardById(id);
        // 数据不存在，抛出异常
        if (article == null) {
            String message = "删除文章失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, message);
        }
        // 调用Mapper的deleteById()执行删除
        int rows = articleMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除文章失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, message);
        }
    }

    @Override
    public void updateInfoById(Long id, ArticleUpdateDTO articleUpdateDTO) {
        // 调用Mapper对象的getStandardById()执行查询
        ArticleDetailsVO queryResult = articleMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改文章详情失败，尝试修改的文章数据不存在！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = articleMapper.countByTitleAndNotId(id, articleUpdateDTO.getTitle());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改文章详情失败，文章名称已经被占用！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, message);
        }
        // 创建Article对象
        Article article = new Article();
        // 将参数对象的属性值复制到以上新创建的Article对象中
        BeanUtils.copyProperties(articleUpdateDTO, article);
        article.setId(id);
        // 调用Mapper的update(Article article)方法执行修改
        int rows = articleMapper.update(article);
        if (rows != 1) {
            String message = "修改文章详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, message);
        }
    }

    @Override
    public ArticleDetailsVO getStandardById(Long id) {
        ArticleDetailsVO queryResult = articleMapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询文章详情失败，文章数据不存在！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public AuthorInfoVO getAuthorInfoByUserId(Long userId) {
        AuthorInfoVO queryResult = articleMapper.getAuthorInfo(userId);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询作者详情失败，用户数据不存在！";
            log.warn(message);
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, message);
        }
        return queryResult;

    }

    @Override
    public List<ArticleListVO> list() {
        List<ArticleListVO> list = articleMapper.list();
        return list;
    }

    @Override
    public void updateViewCountById(Long id) {
        articleMapper.updateViewCountById(id);
    }

    @Override
    public List<ArticleSearchVO> listSearchByKeywords(String keywords) {
        List<ArticleSearchVO> searchVOs = articleMapper.listSearchByKeywords(keywords);
        return searchVOs;
    }
}
